/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.manager.controller;

import com.ipcc.lazyLoading.LazyAbstractDataModel;
import com.ipcc.manager.entity.COMMUNICATION_CHANNEL;
import com.ipcc.manager.entity.IPCC_EXTENSION_GROUPS;
import com.ipcc.manager.entity.IPCC_GROUP_CHANNEL;
import com.ipcc.manager.entity.PARTNER;
import com.ipcc.manager.model.IPCC_EXTENSION_GROUPSModel;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author Administrator
 */
@ViewScoped
public class COMMUNICATION_CHANNELController implements Serializable{
    
    private List<COMMUNICATION_CHANNEL> listCC = new ArrayList<COMMUNICATION_CHANNEL>();
    private LazyDataModel<COMMUNICATION_CHANNEL> lazyCCModel;
    private COMMUNICATION_CHANNEL current = new COMMUNICATION_CHANNEL();
    private String source_numberS;
    private ArrayList<PARTNER> listPartner = new ArrayList<PARTNER>();
    private String group_id;
    private Date from_date;
    private Date to_date;
    private List<IPCC_EXTENSION_GROUPS> listGroup = new ArrayList<IPCC_EXTENSION_GROUPS>();
    private DataModel itemsGroup_Channel;
    private List<IPCC_GROUP_CHANNEL> listIGC = new ArrayList<IPCC_GROUP_CHANNEL>();
    private IPCC_GROUP_CHANNEL currentIGC;
    private IPCC_GROUP_CHANNEL oldCurrentIGC;    
    /**
     * Creates a new instance of COMMUNICATION_CHANNELController
     */
    public COMMUNICATION_CHANNELController() {
        try {                        
            listCC = COMMUNICATION_CHANNEL.getAll("");
            lazyCCModel = new LazyAbstractDataModel<COMMUNICATION_CHANNEL>(listCC);
            listPartner = PARTNER.getAll("");
            listGroup = IPCC_EXTENSION_GROUPS.getAll("");
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
    }

    public LazyDataModel<COMMUNICATION_CHANNEL> getLazyCCModel() {
        return lazyCCModel;
    }

    public void setLazyCCModel(LazyDataModel<COMMUNICATION_CHANNEL> lazyCCModel) {
        this.lazyCCModel = lazyCCModel;
    }

    public IPCC_GROUP_CHANNEL getOldCurrentIGC() {
        return oldCurrentIGC;
    }

    public void setOldCurrentIGC(IPCC_GROUP_CHANNEL oldCurrentIGC) {
        this.oldCurrentIGC = oldCurrentIGC;
    }

    public List<COMMUNICATION_CHANNEL> getListCC() {
        return listCC;
    }

    public void setListCC(List<COMMUNICATION_CHANNEL> listCC) {
        this.listCC = listCC;
    }
    
    public Date getFrom_date() {
        return from_date;
    }

    public void setFrom_date(Date from_date) {
        this.from_date = from_date;
    }

    public Date getTo_date() {
        return to_date;
    }

    public void setTo_date(Date to_date) {
        this.to_date = to_date;
    }

    public IPCC_GROUP_CHANNEL getCurrentIGC() {
        return currentIGC;
    }

    public void setCurrentIGC(IPCC_GROUP_CHANNEL currentIGC) {
        this.currentIGC = currentIGC;
    }

    public DataModel getItemsGroup_Channel() {
        return itemsGroup_Channel;
    }

    public void setItemsGroup_Channel(DataModel itemsGroup_Channel) {
        this.itemsGroup_Channel = itemsGroup_Channel;
    }

    public List<IPCC_GROUP_CHANNEL> getListIGC() {
        return listIGC;
    }

    public void setListIGC(List<IPCC_GROUP_CHANNEL> listIGC) {
        this.listIGC = listIGC;
    }

    public String getGroup_id() {
        return group_id;
    }

    public void setGroup_id(String group_id) {
        this.group_id = group_id;
    }

    public List<IPCC_EXTENSION_GROUPS> getListGroup() {
        return listGroup;
    }

    public void setListGroup(List<IPCC_EXTENSION_GROUPS> listGroup) {
        this.listGroup = listGroup;
    }

    public ArrayList<PARTNER> getListPartner() {
        return listPartner;
    }

    public void setListPartner(ArrayList<PARTNER> listPartner) {
        this.listPartner = listPartner;
    }

    public String getSource_numberS() {
        return source_numberS;
    }

    public void setSource_numberS(String source_numberS) {
        this.source_numberS = source_numberS;
    }  

    public COMMUNICATION_CHANNEL getCurrent() {
        return current;
    }

    public void setCurrent(COMMUNICATION_CHANNEL current) {
        this.current = current;
    }   

    public String prepareRemove(COMMUNICATION_CHANNEL cc){
        try{
            current = (COMMUNICATION_CHANNEL) cc.clone();
        }
        catch(Exception e){
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
        return "";
    }
    
    public String remove() {
        try {
            if (current.remove()) {
                listCC.remove(current);                
                lazyCCModel = new LazyAbstractDataModel<COMMUNICATION_CHANNEL>(listCC);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xóa bản ghi thành công"));
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí"));
            }            
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
        return "";
    }

    public String search() {
        try {
            listCC = COMMUNICATION_CHANNEL.getAll(" where source_number like '%" + source_numberS + "%' ");
            lazyCCModel = new LazyAbstractDataModel<COMMUNICATION_CHANNEL>(listCC);
            int i = listCC.size();
            if (i > 0) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Có " + i + " bản ghi được tìm thấy!"));
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không có bản ghi nào được tìm thấy!"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
        return "";
    }

    public boolean checkInput() {
        if (current.getSource_number() == null || current.getSource_number().equalsIgnoreCase("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Trường Source number không được để trống!"));
            return false;
        }
        return true;
    }

    public boolean checkInputEdit() {
        if (current.getSource_number() == null || current.getSource_number().equalsIgnoreCase("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Trường Source number không được để trống!"));
            return false;
        }
        return true;
    }

    public String updateInsert(){
        current = new COMMUNICATION_CHANNEL();
        return "";
    }
    
    public String insert() {
        if (checkInput()) {            
            try {
                if (current.insert()) {
                    listCC.add(current);
                    lazyCCModel = new LazyAbstractDataModel<COMMUNICATION_CHANNEL>(listCC);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Thêm bản ghi thành công!"));
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
                }
            } catch (Exception e) {
                e.printStackTrace();
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
            }
            finally{
                current = new COMMUNICATION_CHANNEL();
            }
        }
        return "";
    }

    public String prepareEdit(COMMUNICATION_CHANNEL cc) {
        try{
            current = (COMMUNICATION_CHANNEL) cc.clone();
        }
        catch(Exception e){
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
        return "";
    }

    public String update() {
        if (checkInputEdit()) {
            try {
                if (current.update()) {                    
                    for(int i=0;i<listCC.size();i++){
                        if(listCC.get(i).getId().equals(current.getId())){
                            listCC.set(i, current);
                            break;
                        }
                    }
                    lazyCCModel = new LazyAbstractDataModel<COMMUNICATION_CHANNEL>(listCC);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Cập nhật bản ghi thành công!"));
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
                }
            } catch (Exception e) {
                e.printStackTrace();
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
            }
            finally{
                current = new COMMUNICATION_CHANNEL();
            }
        }
        return "";
    }

    public String prepareSetGroup(COMMUNICATION_CHANNEL cc) {
        try {
            current = (COMMUNICATION_CHANNEL) cc.clone();
            listIGC = IPCC_GROUP_CHANNEL.getAllBySource_number(cc.getSource_number());
            itemsGroup_Channel = new ListDataModel(listIGC);
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
        return "";
    }

    public boolean checkInputAddGroup() {
        if (from_date == null || from_date.toString().equalsIgnoreCase("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không được để trống From date!"));
            return false;
        }
        if(to_date != null && to_date.compareTo(from_date) < 0){            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("To date không được nhỏ hơn From date!"));
            return false;
        }
        return true;
    }

    public String updateInsertGroup(){
        currentIGC = new IPCC_GROUP_CHANNEL();        
        return "";
    }
    
    public String insertGroup() {
        try {
            if (checkInputAddGroup()) {                
                currentIGC = new IPCC_GROUP_CHANNEL();
                currentIGC.setGroup_id(group_id);
                currentIGC.setSource_number(current.getSource_number());
                currentIGC.setFrom_date(Date2String(from_date));
                if (to_date == null || to_date.toString().equalsIgnoreCase("")) {
                    currentIGC.setTo_date("");
                } else {
                    currentIGC.setTo_date(Date2String(to_date));
                }
                if (currentIGC.insert()) {
                    listIGC.add(currentIGC);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Cập nhật bản ghi thành công!"));
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
        return "";
    }

    public String Date2String(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        return sdf.format(date);
    }

    public Date String2Date(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        return sdf.parse(date);
    }

    public String updateEditGroup() {
        currentIGC = (IPCC_GROUP_CHANNEL) itemsGroup_Channel.getRowData();
        oldCurrentIGC = currentIGC;
        group_id = currentIGC.getGroup_id();
        try {
            from_date = String2Date(currentIGC.getFrom_date());
            if (currentIGC.getTo_date() != null && !currentIGC.getTo_date().equalsIgnoreCase("")) {
                to_date = String2Date(currentIGC.getTo_date());
            }
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
        return "";
    }

    public String updateGroup() {
        try {
            if (checkInputAddGroup()) {                
                currentIGC.setGroup_id(group_id);
                currentIGC.setFrom_date(Date2String(from_date));
                if (to_date == null || to_date.toString().equalsIgnoreCase("")) {
                    currentIGC.setTo_date("");
                } else {
                    currentIGC.setTo_date(Date2String(to_date));
                }
                if (currentIGC.update()) {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Cập nhật bản ghi thành công!"));
                } else {
                    currentIGC = oldCurrentIGC;
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
                }
            }
        } catch (Exception e) {
            currentIGC = oldCurrentIGC;
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
        return "";
    }

    public String updateRemoveGroup(){
        currentIGC = (IPCC_GROUP_CHANNEL) itemsGroup_Channel.getRowData();
        return "";
    }
    
    public String removeGroup() {
        try {
            if (currentIGC.remove()) {
                listIGC.remove(currentIGC);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xóa bản ghi thành công!"));
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xảy ra lỗi trong quá trình xử lí!"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
        return "";
    }
    public String clear(){
        from_date = null;
        to_date = null;
        return "";
    }
    
    public String groupGetName(String id){
        try{
            return IPCC_EXTENSION_GROUPSModel.getNameById(id);        
        }catch(Exception e){
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
        }
        return "";
    }
}
